﻿Imports MapWinGIS
Imports System.IO


Public Class GLayer

    Dim mapwin As AxMapWinGIS.AxMap
    Dim sfList As New Dictionary(Of Integer, Shapefile)
    Dim nLayers As New Dictionary(Of String, Integer)

    'Dim sfListA As New Dictionary(Of Integer, Shapefile)

    Dim sfListA As New Dictionary(Of Integer, Object)
    Dim nLayersA As New Dictionary(Of String, Integer)

    '关于直接选取的选择模型,这里表示所有选择了的图形
    Public sShapes As New Dictionary(Of Integer, Integer())
    Dim ext As New MapWinGIS.Extents '上一次的选择框，实在没办法啊


    Public sColorU32 As UInt32 = 255





    Public Sub New(ByRef win As AxMapWinGIS.AxMap)
        mapwin = win

    End Sub

    Public Sub InitSystem()

    End Sub

    Public Sub AddLayer(ByVal lname As String, ByVal type As MapWinGIS.ShpfileType)



        Dim iLayer As Integer
        Dim sf As New Shapefile
        Dim strPath(3) As String


        strPath(0) = "C:\temp\" + lname + ".shp"
        strPath(1) = "C:\temp\" + lname + ".dbf"
        strPath(2) = "C:\temp\" + lname + ".shx"

        If System.IO.File.Exists(strPath(0)) Then
            System.IO.File.Delete(strPath(0))
            System.IO.File.Delete(strPath(1))
            System.IO.File.Delete(strPath(2))

        End If

        sf.CreateNew(strPath(0), type)




        Dim f As New Field

        f = New Field
        f.Name = "Name"
        f.Type = FieldType.STRING_FIELD
        f.Width = 50

        sf.StartEditingShapes(True)
        sf.EditInsertField(f, 0)
        sf.StopEditingShapes(True, True)


        '加入载入的逻辑

        Dim strSave As String = "C:\test\" + lname + ".shp"

        strPath(0) = "C:\test\" + lname + ".shp"
        strPath(1) = "C:\test\" + lname + ".dbf"
        strPath(2) = "C:\test\" + lname + ".shx"

        If System.IO.File.Exists(strSave) Then

            System.IO.File.Delete(strPath(0))
            System.IO.File.Delete(strPath(1))
            System.IO.File.Delete(strPath(2))

        End If

        sf.SaveAs(strSave)

        Dim sfUse As New Shapefile
        sfUse.Open(strSave)


        iLayer = mapwin.AddLayer(sfUse, True)

        sfList.Add(iLayer, sfUse)
        nLayers.Add(lname, iLayer)


        '添加内部的层次查找模型
        mapwin.set_LayerKey(iLayer, lname)




    End Sub
    Public Sub AddAsistLayer(ByVal lname As String, ByVal type As MapWinGIS.ShpfileType)
        Dim iLayer As Integer
        Dim sf As New Shapefile
        Dim strPath(3) As String
        strPath(0) = "C:\temp\" + lname + ".shp"
        strPath(0) = "C:\temp\" + lname + ".dbf"
        strPath(0) = "C:\temp\" + lname + ".shx"

        If System.IO.File.Exists(strPath(0)) Then
            System.IO.File.Delete(strPath(0))
            System.IO.File.Delete(strPath(1))
            System.IO.File.Delete(strPath(2))

        End If

        sf.CreateNew(strPath(0), type)
        Dim f As New Field

        f = New Field
        f.Name = "Name"
        f.Type = FieldType.STRING_FIELD
        f.Width = 50

        sf.StartEditingShapes(True)
        sf.EditInsertField(f, 0)
        sf.StopEditingShapes(True, True)



        '加入载入的逻辑

        Dim strSave As String = "C:\test\" + lname + ".shp"

        strPath(0) = "C:\test\" + lname + ".shp"
        strPath(1) = "C:\test\" + lname + ".dbf"
        strPath(2) = "C:\test\" + lname + ".shx"

        If System.IO.File.Exists(strSave) Then

            System.IO.File.Delete(strPath(0))
            System.IO.File.Delete(strPath(1))
            System.IO.File.Delete(strPath(2))

        End If

        sf.SaveAs(strSave)

        Dim sfUse As New Shapefile
        sfUse.Open(strSave)

        iLayer = mapwin.AddLayer(sfUse, True)



        sfListA.Add(iLayer, sfUse)
        nLayersA.Add(lname, iLayer)

        '添加内部的层次查找模型
        mapwin.set_LayerKey(iLayer, lname)




    End Sub
    Public Sub AddAsistLayer(ByVal lname As String, ByVal fname As String)

        Dim iLayer As Integer
        Dim img As New MapWinGIS.Image

        ' img.Open(fname, ImageType.DEM_FILE)
        img.Open(fname)

        ' Dim str As String = img.GetProjection()
        ' img.SetProjection("+proj=utm +zone=39 +ellps=WGS84 +units=m +no_defs ")

        img.ImageColorScheme = PredefinedColorScheme.SummerMountains
        iLayer = mapwin.AddLayer(img, True)

        '这里使用的是图像层，这个有点麻烦

        sfListA.Add(iLayer, img)

        nLayersA.Add(lname, iLayer)


        '添加内部的层次查找模型
        mapwin.set_LayerKey(iLayer, lname)



    End Sub

    Public Sub AddAsistLayer(ByVal lname As String, ByVal fname As String, ByVal proj As String)

        Dim iLayer As Integer
        Dim img As New MapWinGIS.Image

        ' img.Open(fname, ImageType.DEM_FILE)
        img.Open(fname)

        







        'img.ImageColorScheme = PredefinedColorScheme.SummerMountains
        iLayer = mapwin.AddLayer(img, True)


        '这里使用的是图像层，这个有点麻烦
        'sfListA.Add(iLayer)  

        nLayersA.Add(lname, iLayer)


    End Sub

    Public Sub ReadLayer(ByVal lname As String, ByVal fname As String)
        Dim iLayer As Integer
        Dim sf As New Shapefile
        sf.Open(fname)

        iLayer = mapwin.AddLayer(sf, True)

        sfListA.Add(iLayer, sf)
        nLayersA.Add(lname, iLayer)

        '添加内部的层次查找模型
        mapwin.set_LayerKey(iLayer, lname)


    End Sub

    Public Sub MoveLayer(ByVal initP As Integer, ByVal targetP As Integer)

        mapwin.MoveLayer(initP, targetP)



    End Sub

    Public Sub RemoveLayer(ByVal lname As String)
        Dim iLayer As Integer
        iLayer = Me.GetLayerId(lname)

        mapwin.RemoveLayer(iLayer)

        nLayers.Remove(lname)
        sfList.Remove(iLayer)


    End Sub
    Public Sub RemoveAsistLayer(ByVal lname As String)

        Dim iLayer As Integer
        iLayer = Me.GetAsistLayerId(lname)

        mapwin.RemoveLayer(iLayer)

        nLayersA.Remove(lname)
        sfListA.Remove(iLayer)

        '  mapwin.set_ShapeLayerPointSize()


    End Sub

    Public Function GetLayer(ByVal iLayer As Integer) As Shapefile

        Dim sf As Shapefile
        Dim b As Boolean = sfList.TryGetValue(iLayer, sf)

        If b Then
            Return sf
        Else
            Return Nothing

        End If

    End Function

    Public Function GetLayer(ByVal str As String) As Shapefile
        Dim sf As Shapefile


        If nLayers.ContainsKey(str) Then
            sf = Me.GetLayer(nLayers.Item(str))
            Return sf
        Else
            Return Nothing

        End If




    End Function

    Public Function GetAsistLayer(ByVal iLayer As Integer) As Shapefile
        Dim sf As Shapefile
        Dim b As Boolean = sfListA.TryGetValue(iLayer, sf)

        If b Then
            Return sf
        Else
            Return Nothing

        End If
    End Function

    Public Function GetAsistLayer(ByVal str As String) As Shapefile
        Dim sf As Shapefile


        If nLayersA.ContainsKey(str) Then
            sf = Me.GetAsistLayer(nLayersA.Item(str))
            Return sf
        Else
            Return Nothing

        End If
    End Function

    Public Function GetLayerName(ByVal iLayer As Integer) As String

        Dim v As Integer
        Dim k As String

        Dim pair As KeyValuePair(Of String, Integer)
        For Each pair In nLayers
            v = pair.Value
            k = pair.Key

            If v = iLayer Then
                Return k
            End If

        Next

        Return ""
       


    End Function
    Public Function GetLayerId(ByVal str As String) As Integer

        If nLayers.ContainsKey(str) Then
            Return nLayers.Item(str)
        Else
            Return -1
        End If

    End Function

    Public Function GetAsistLayerId(ByVal str As String) As Integer
        If nLayersA.ContainsKey(str) Then
            Return nLayersA.Item(str)
        Else
            Return -1
        End If
    End Function

    Public Sub SetAllLayerColor(ByVal col As Drawing.Color)


        Dim uColor As UInt32
        uColor = RGB(col.R, col.G, col.B)


        Dim list As New List(Of Integer)(sfList.Keys)

        Dim i As Integer
        For Each i In list
            mapwin.set_ShapeLayerPointColor(i, uColor)
            mapwin.set_ShapeLayerLineColor(i, uColor)


        Next



    End Sub

    Public Sub RemoveLayerShape(ByVal lname As String, ByVal iShape As Integer)

        Dim sfData As Shapefile = Me.GetLayer(lname)
        If sfData Is Nothing Then
            Return
        End If

        sfData.StartEditingShapes(True)

        sfData.EditDeleteShape(iShape)



        sfData.StopEditingShapes()

        mapwin.Redraw()

    End Sub

    Public Sub RemoveLayerShape(ByVal iLayer As Integer, ByVal iShape As Integer)

        Dim sfData As Shapefile = Me.GetLayer(iLayer)
        If sfData Is Nothing Then
            Return
        End If


        sfData.StartEditingShapes(True)

        sfData.EditDeleteShape(iShape)

        sfData.StopEditingShapes()

        mapwin.Redraw()

    End Sub

    Public Sub RemoveLayerShape(ByVal lname As String, ByVal sname As String)
        Dim iShape As Integer = GetLayerShape(lname, sname)
        If iShape <> -1 Then
            RemoveLayerShape(lname, iShape)
        Else
            ' MessageBox.Show(iShape.ToString())

        End If
    End Sub
    '取得对应名称的shape文件
    Public Function GetLayerShape(ByVal lname As String, ByVal sname As String) As Integer


        Dim sfData As Shapefile = Me.GetLayer(lname)
        If sfData Is Nothing Then
            Return -1
        End If
        Dim n As Integer = sfData.NumShapes
        For i As Integer = 0 To n - 1

            If (sfData.CellValue(0, i).ToString() = sname) Then
                Return i
            End If

        Next


        Return -1


    End Function

    Public Function GetAsistLayerShapeGroup(ByVal lname As String, ByVal sname As String) As List(Of Integer)

        Dim mList As New List(Of Integer)


        Dim sfData As Shapefile = Me.GetAsistLayer(lname)
        If sfData Is Nothing Then
            Return mList
        End If
        Dim n As Integer = sfData.NumShapes
        For i As Integer = 0 To n - 1

            If (sfData.CellValue(0, i).ToString() = sname) Then
                mList.Add(i)
            End If

        Next


        Return mList


    End Function

    Public Function GetLayerShapeInt(ByVal lname As String, ByVal iShape As Integer) As String

        Dim sfData As Shapefile = Me.GetLayer(lname)

        Dim n As Integer = sfData.NumShapes

        If iShape >= n Then

            Return -1

        End If


        Return sfData.CellValue(0, iShape).ToString()


    End Function






    Public Sub AddLayerShapePoint(ByVal name As String, ByVal pt1 As MapWinGIS.Point, ByVal sname As String, ByVal bLbl As Boolean, ByVal cl As Drawing.Color)

        Dim sfPoint As Shapefile
        sfPoint = Me.GetLayer(name)

        sfPoint.StartEditingShapes(True)





        Dim shp As New Shape
        shp.ShapeType = ShpfileType.SHP_POINT
        Dim ptIndex As Integer = 0

        shp.InsertPoint(pt1, ptIndex)




        Dim shpIndex As Integer = sfPoint.NumShapes

        sfPoint.EditInsertShape(shp, shpIndex)

        sfPoint.EditCellValue(0, shpIndex, sname)

        sfPoint.StopEditingShapes()

        '下面询问是否需要绘制标签
        If bLbl Then

            Dim ucl As UInt32 = RGB(cl.R, cl.G, cl.B)

            Me.DrawShapeName(name, shpIndex, 0, ucl)
        End If

        mapwin.Redraw()

    End Sub
    Public Sub AddLayerShapeLine(ByVal name As String, ByVal pt1 As MapWinGIS.Point, ByVal pt2 As MapWinGIS.Point, ByVal sname As String, ByVal bLbl As Boolean, ByVal cl As Drawing.Color)

        Dim sfLine As Shapefile
        sfLine = Me.GetLayer(name)

        sfLine.StartEditingShapes(True)





        Dim shp As New Shape
        shp.ShapeType = ShpfileType.SHP_POLYLINE
        Dim ptIndex As Integer = 0

        shp.InsertPoint(pt1, ptIndex)
        shp.InsertPoint(pt2, ptIndex)



        Dim shpIndex As Integer = sfLine.NumShapes

        sfLine.EditInsertShape(shp, shpIndex)

        sfLine.EditCellValue(0, shpIndex, sname)

        sfLine.StopEditingShapes()


        '下面询问是否需要绘制标签
        If bLbl Then


            Dim ucl As UInt32 = RGB(cl.R, cl.G, cl.B)

            Me.DrawShapeName(name, shpIndex, 0, ucl)
        End If

        mapwin.Redraw()

    End Sub
    Public Function BuildCirclePoints(ByVal p1 As Drawing.Point, ByVal r As Double) As MapWinGIS.Point()
        Dim pt As New List(Of Drawing.Point)
        Dim ptOld As New List(Of Drawing.Point)

        Dim p3 As New Drawing.Point
        Dim p4 As New Drawing.Point




        For i As Integer = (-1 * Math.Floor(r)) To Math.Floor(r)

            p3.X = p1.X + i
            p3.Y = Math.Sqrt(r ^ 2 - i ^ 2) + p1.Y

            p4.X = p1.X + i
            p4.Y = (-1) * Math.Sqrt(r ^ 2 - i ^ 2) + p1.Y

            pt.Add(p3)
            ptOld.Add(p4)






        Next

        '能否给出一些低精度的值

        'For i As Integer = (-1 * Math.Floor(r)) To Math.Floor(r) Step 4

        '    p3.X = p1.X + i
        '    p3.Y = Math.Sqrt(r ^ 2 - i ^ 2) + p1.Y

        '    p4.X = p1.X + i
        '    p4.Y = (-1) * Math.Sqrt(r ^ 2 - i ^ 2) + p1.Y

        '    pt.Add(p3)
        '    ptOld.Add(p4)






        'Next

        ptOld.Reverse()
        pt.AddRange(ptOld)


        '加入最后一个点，行程封闭的曲线

        Dim pOld As New Drawing.Point

        pOld = pt.Item(0)

        pt.Add(pOld)

        Dim plist(pt.Count) As MapWinGIS.Point


        Dim temp As MapWinGIS.Point

        Dim j As Integer = 0

        For Each p As Drawing.Point In pt

            temp = New MapWinGIS.Point
            mapwin.PixelToProj(p.X, p.Y, temp.x, temp.y)
            plist(j) = temp
            j = j + 1

        Next

        Return plist


    End Function

    Public Sub addLayerShapeCirclePoly(ByVal name As String, ByVal ptlist() As MapWinGIS.Point, ByVal sname As String, ByVal bLbl As Boolean, ByVal cl As Drawing.Color)
        Dim p1 As New Drawing.Point
        mapwin.ProjToPixel(ptlist(0).x, ptlist(0).y, p1.X, p1.Y)

        Dim p2 As New Drawing.Point
        mapwin.ProjToPixel(ptlist(1).x, ptlist(1).y, p2.X, p2.Y)

        Dim r As Double = Math.Sqrt((p1.X - p2.X) ^ 2 + (p1.Y - p2.Y) ^ 2)

        Dim mylist() As MapWinGIS.Point = Me.BuildCirclePoints(p1, r)




        '前面只是为了画圆而作的预处理工作

        Dim sfPoly As Shapefile
        sfPoly = Me.GetLayer(name)


        sfPoly.StartEditingShapes(True)



        '注意下面都是用mylist


        Dim shp As New Shape
        shp.ShapeType = ShpfileType.SHP_POLYLINE
        Dim ptIndex As Integer = 0

        Dim n As Integer = mylist.Length

        '  Dim pTemp As MapWinGIS.Point

        For i As Integer = 1 To n

            'pTemp = New MapWinGIS.Point

            shp.InsertPoint(mylist(i - 1), ptIndex)


        Next




        Dim shpIndex As Integer = sfPoly.NumShapes

        sfPoly.EditInsertShape(shp, shpIndex)

        sfPoly.EditCellValue(0, shpIndex, sname)

        sfPoly.StopEditingShapes()

        '下面询问是否需要绘制标签
        If bLbl Then

            Dim ucl As UInt32 = RGB(cl.R, cl.G, cl.B)

            Me.DrawShapeName(name, shpIndex, 0, ucl)
        End If

        mapwin.Redraw()


    End Sub

    Public Sub AddLayerShapePolyLine(ByVal name As String, ByVal ptlist() As MapWinGIS.Point, ByVal sname As String, ByVal bLbl As Boolean, ByVal cl As Drawing.Color, Optional ByVal bEnclosed As Boolean = True)
        If (bEnclosed = True) Then
            Dim nl As Integer = ptlist.Length
           

            ReDim Preserve ptlist(nl + 1)

            ptlist(nl) = ptlist(0)


            'Dim temp(n + 1) As MapWinGIS.Point

            'For i As Integer = 1 To n

            '    shp.InsertPoint(ptlist(i - 1), ptIndex)


            'Next

        End If




        Dim sfPoly As Shapefile
        sfPoly = Me.GetLayer(name)


        sfPoly.StartEditingShapes(True)





        Dim shp As New Shape
        shp.ShapeType = ShpfileType.SHP_POLYLINE
        Dim ptIndex As Integer = 0

        Dim n As Integer = ptlist.Length



        For i As Integer = 1 To n

            shp.InsertPoint(ptlist(i - 1), ptIndex)


        Next




        Dim shpIndex As Integer = sfPoly.NumShapes

        sfPoly.EditInsertShape(shp, shpIndex)

        sfPoly.EditCellValue(0, shpIndex, sname)

        sfPoly.StopEditingShapes()

        '下面询问是否需要绘制标签
        If bLbl Then

            Dim ucl As UInt32 = RGB(cl.R, cl.G, cl.B)

            Me.DrawShapeName(name, shpIndex, 0, ucl)
        End If

        mapwin.Redraw()

    End Sub

    '这个函数目前已经废弃
    Public Sub AddLayerShapePolygon(ByVal name As String, ByVal ptlist() As MapWinGIS.Point, ByVal sname As String, ByVal bLbl As Boolean, ByVal cl As Drawing.Color, ByVal clipName As String, ByVal ClipList As List(Of MapWinGIS.Point()), Optional ByVal bEnclosed As Boolean = True)
        If (bEnclosed = True) Then
            Dim nl As Integer = ptlist.Length


            ReDim Preserve ptlist(nl + 1)

            ptlist(nl) = ptlist(0)


            'Dim temp(n + 1) As MapWinGIS.Point

            'For i As Integer = 1 To n

            '    shp.InsertPoint(ptlist(i - 1), ptIndex)


            'Next

        End If




        Dim sfPoly As Shapefile
        sfPoly = Me.GetAsistLayer(name)


        sfPoly.StartEditingShapes(True)





        Dim shp As New Shape
        shp.ShapeType = ShpfileType.SHP_POLYGON
        Dim ptIndex As Integer = 0

        Dim n As Integer = ptlist.Length



        For i As Integer = 1 To n

            shp.InsertPoint(ptlist(i - 1), ptIndex)


        Next


        Dim mshp As MapWinGIS.Shape = shp

        '本浓度剔除
        If ClipList.Count > 0 Then

            ptIndex = 0

            Dim u As New MapWinGIS.Utils
            For Each cpoints As MapWinGIS.Point() In ClipList

                If (u.PointInPolygon(mshp, cpoints(0))) Then
                    Dim clipShp As New Shape
                    clipShp.ShapeType = ShpfileType.SHP_POLYGON

                    Dim k As Integer = cpoints.Length

                    For i As Integer = 1 To k

                        clipShp.InsertPoint(cpoints(i - 1), ptIndex)


                    Next

                    '加入第一个点的情况

                    clipShp.InsertPoint(cpoints(0), ptIndex)



                    mshp = u.ClipPolygon(MapWinGIS.PolygonOperation.DIFFERENCE_OPERATION, mshp, clipShp)

                End If

            Next

        End If





        '高浓度剔除
        If clipName <> "" Then

            Dim Myuti As New MapWinGIS.Utils

            Dim cshps As New List(Of Integer)

            cshps = Me.GetAsistLayerShapeGroup(name, clipName)


            Dim tempshp As New Shape
            If cshps.Count > 0 Then
                For Each j As Integer In cshps



                    tempshp = sfPoly.Shape(j)



                    mshp = Myuti.ClipPolygon(MapWinGIS.PolygonOperation.DIFFERENCE_OPERATION, mshp, tempshp)










                Next



            End If



        End If







        Dim shpIndex As Integer = sfPoly.NumShapes

        sfPoly.EditInsertShape(mshp, shpIndex)

        sfPoly.EditCellValue(0, shpIndex, sname)

        sfPoly.StopEditingShapes()

        '下面询问是否需要绘制标签
        If bLbl Then

            'Dim ucl As UInt32 = RGB(cl.R, cl.G, cl.B)

            'Me.DrawAsistShapeName(name, shpIndex, 0, ucl)
        End If

        mapwin.Redraw()

    End Sub
    Public Sub AddLayerShapePolygon(ByVal name As String, ByVal mshp As MapWinGIS.Shape, ByVal sname As String, ByVal bLbl As Boolean, ByVal cl As Drawing.Color)

        Dim sfPoly As Shapefile
        sfPoly = Me.GetAsistLayer(name)


        sfPoly.StartEditingShapes(True)




        Dim shpIndex As Integer = sfPoly.NumShapes

        sfPoly.EditInsertShape(mshp, shpIndex)

        sfPoly.EditCellValue(0, shpIndex, sname)

        sfPoly.StopEditingShapes()

        '下面询问是否需要绘制标签,m
        'If bLbl Then

        '    Dim ucl As UInt32 = RGB(cl.R, cl.G, cl.B)

        '    Me.DrawAsistShapeName(name, shpIndex, 0, ucl)
        'End If

        mapwin.Redraw()

    End Sub
    Public Function GetShapePolygonWithoutHole(ByVal ptlist() As MapWinGIS.Point, ByVal ClipList As List(Of MapWinGIS.Point())) As Shape

        '外边点的添加，使封闭
        Dim nl As Integer = ptlist.Length



        ReDim Preserve ptlist(nl + 1)

        ptlist(nl) = ptlist(0)




        Dim shp As New Shape
        shp.ShapeType = ShpfileType.SHP_POLYGON
        Dim ptIndex As Integer = 0

        Dim n As Integer = ptlist.Length



        For i As Integer = 1 To n

            shp.InsertPoint(ptlist(i - 1), ptIndex)


        Next


        Dim mshp As MapWinGIS.Shape = shp

        '本浓度剔除
        If ClipList.Count > 0 Then

            ptIndex = 0

            Dim u As New MapWinGIS.Utils
            For Each cpoints As MapWinGIS.Point() In ClipList

                If (u.PointInPolygon(mshp, cpoints(0))) Then
                    Dim clipShp As New Shape
                    clipShp.ShapeType = ShpfileType.SHP_POLYGON

                    Dim k As Integer = cpoints.Length

                    For i As Integer = 1 To k

                        clipShp.InsertPoint(cpoints(i - 1), ptIndex)


                    Next

                    '加入第一个点的情况

                    clipShp.InsertPoint(cpoints(0), ptIndex)



                    mshp = u.ClipPolygon(MapWinGIS.PolygonOperation.DIFFERENCE_OPERATION, mshp, clipShp)

                End If

            Next

        End If



        Return mshp



    End Function


   



    Public Sub SelectShapes(ByVal ext As MapWinGIS.Extents)

        '遍历所有的层，进行选择操作

        Me.ResetSelect()

        '这里用了多次，哈哈
        Me.ext = ext


        Dim sf As Shapefile
        Dim iLayer As Integer
        Dim selectedshapes() As Integer

        Dim pair As KeyValuePair(Of Integer, Shapefile)
        For Each pair In sfList
            sf = pair.Value
            iLayer = pair.Key

            '只选择可见层
            If mapwin.get_LayerVisible(iLayer) Then



                If sf.SelectShapes(ext, 0, MapWinGIS.SelectMode.INTERSECTION, selectedshapes) = True Then

                    Dim I As Integer



                    For I = 0 To selectedshapes.Length - 1
                        ' since the select box is smaller than the map, we already know the shapes selected are in the right place.
                        ' This tests each shape to make sure that they have not had their "visible" property set to false.
                        If mapwin.get_ShapeVisible(iLayer, selectedshapes(I)) Then





                            ' mapwin.set_ShapeFillColor(iLayer, selectedshapes(I), sColorU32)
                            mapwin.set_ShapePointColor(iLayer, selectedshapes(I), sColorU32)
                            mapwin.set_ShapeLineColor(iLayer, selectedshapes(I), sColorU32)

                        End If

                    Next

                    '加入已选择列表
                    sShapes.Add(iLayer, selectedshapes)


                End If



            End If



        Next












    End Sub


    '这里用于给颜色回归
    Public Sub ResetSelect()

        ' ByRef ps As List(Of GPropLayer)

        Dim iLayer As Integer
        Dim pair As KeyValuePair(Of Integer, Shapefile)
        For Each pair In sfList
            '去掉选择了的物体的颜色
            iLayer = pair.Key


            Dim col As System.Drawing.Color = mapwin.get_ShapeLayerPointColor(iLayer)

            Dim col2 As System.Drawing.Color = mapwin.get_ShapeLayerLineColor(iLayer)

            Dim originalColor As UInt32 = col.B * 256 * 256 + col.G * 256 + col.R

            Dim originalColor2 As UInt32 = col2.B * 256 * 256 + col2.G * 256 + col2.R

            ' re-color the colored shapes the same color as the layer was colored originally




            'mapwin.set_ShapeLayerFillColor(iLayer, originalColor)
            mapwin.set_ShapeLayerLineColor(iLayer, originalColor2)
            mapwin.set_ShapeLayerPointColor(iLayer, originalColor)

            sShapes.Remove(iLayer)
        Next
    End Sub


    '这里用了一个比较无耻的方法，哈哈

    Public Sub RemoveSelectedShape()

        If sShapes.Count < 1 Then
            Return
        End If






        '下面是遍历所有选择了的图形的方法，建议保留

        Dim sf As Shapefile


        Dim iLayer As Integer
        Dim selectedshapes() As Integer

        Dim pair As KeyValuePair(Of Integer, Integer())
        For Each pair In sShapes
            selectedshapes = pair.Value
            iLayer = pair.Key





            Me.RemoveLayerShape(iLayer, selectedshapes(0))


        Next




        '清空选择
        ' sShapes.Clear()
        Me.SelectShapes(Me.ext)




    End Sub

    Public Sub GetFirstSelectedShape(ByRef lay As Integer, ByRef shp As Integer)
        If sShapes.Count < 1 Then
            lay = -1
            shp = -1
            Return
        End If

        Dim sf As Shapefile


        Dim iLayer As Integer
        Dim selectedshapes() As Integer

        Dim pair As KeyValuePair(Of Integer, Integer())
        For Each pair In sShapes
            selectedshapes = pair.Value
            iLayer = pair.Key

            ' Me.RemoveLayerShape(iLayer, selectedshapes(0))

            lay = iLayer
            shp = selectedshapes(0)


        Next


    End Sub


    Public Sub SetLayerVisible(ByVal name As String, ByVal b As Boolean)
        Dim i As Integer
        i = GetLayerId(name)
        If i = -1 Then
            i = GetAsistLayerId(name)
        End If
        mapwin.set_LayerVisible(i, b)
        ' mapwin.set_LayerVisible(GetLayerId(name), b)

    End Sub

    Public Function GetFromAllLayerId(ByVal name As String) As Integer
        Dim i As Integer
        i = GetLayerId(name)
        If i = -1 Then
            i = GetAsistLayerId(name)
        End If

        Return i

    End Function


    Public Sub ZoomToLayer(ByVal name As String)
        Dim i As Integer
        i = GetLayerId(name)
        If i = -1 Then
            i = GetAsistLayerId(name)
        End If


        If i = -1 Then
            Return
        End If

        mapwin.ZoomToLayer(i)





    End Sub

    'Public Sub AddLayerShape(ByVal name As String, ByVal shp As BaseShape)

    'End Sub

    Public Sub DrawAllShapeName(ByVal ln As String, ByVal fldIndex As Integer, ByVal ucl As UInt32)

        Dim sfData As Shapefile = Me.GetLayer(ln)
        Dim iLayer As Integer = Me.GetLayerId(ln)

        Dim str As String

        Dim pt As MapWinGIS.Point
        Dim uclr As UInt32 = ucl



        For i As Integer = 0 To sfData.NumShapes - 1

            If (sfData.NumFields - 1) >= fldIndex Then

                str = sfData.CellValue(fldIndex, i)

                pt = sfData.QuickPoint(i, 0)

                mapwin.AddLabelEx(iLayer, str, uclr, pt.x, pt.y, tkHJustification.hjCenter, 0)

            End If

        Next




    End Sub

    Public Sub DrawShapeName(ByVal ln As String, ByVal iShape As Integer, ByVal fldIndex As Integer, ByVal ucl As UInt32)

        Dim sfData As Shapefile = Me.GetLayer(ln)
        Dim iLayer As Integer = Me.GetLayerId(ln)

        Dim str As String

        Dim pt As MapWinGIS.Point
        Dim uclr As UInt32 = ucl





        If (sfData.NumFields - 1) >= fldIndex Then

            str = sfData.CellValue(fldIndex, iShape)

            pt = sfData.QuickPoint(iShape, 0)

            mapwin.AddLabelEx(iLayer, str, uclr, pt.x, pt.y, tkHJustification.hjCenter, 0)

        End If





    End Sub

    Public Sub DrawAsistShapeName(ByVal ln As String, ByVal iShape As Integer, ByVal fldIndex As Integer, ByVal ucl As UInt32)

        Dim sfData As Shapefile = Me.GetAsistLayer(ln)
        Dim iLayer As Integer = Me.GetAsistLayerId(ln)

        Dim str As String

        Dim pt As MapWinGIS.Point
        Dim uclr As UInt32 = ucl





        If (sfData.NumFields - 1) >= fldIndex Then

            str = sfData.CellValue(fldIndex, iShape)

            pt = sfData.QuickPoint(iShape, 0)

            mapwin.AddLabelEx(iLayer, str, uclr, pt.x, pt.y, tkHJustification.hjCenter, 0)

        End If





    End Sub

    Public Sub DrawContourLine()

    End Sub


    Public Sub DrawGridLine(ByVal g As GridProp)
        If g.IsShowXLineProperty Then
            Me.DrawGridLineX(g)
        End If

        If g.IsShowYlineProperty Then
            Me.DrawGridLineY(g)
        End If

    End Sub

    '一下将横向网格线和纵向网格线分开绘制
    Public Sub DrawGridLineX(ByVal g As GridProp)
        Dim name As String = g.LnameProperty
        Dim xOrg As Double = g.LBPointXAProperty
        Dim yOrg As Double = g.LBPointYAProperty
        Dim xcells As Integer = g.XCellsProperty
        Dim ycells As Integer = g.YCellsProperty

        'Dim gridspace As Double
        Dim xspace As Double = g.XunitLProperty
        Dim yspace As Double = g.YuintLProperty


        Dim sfGrid As Shapefile

        sfGrid = Me.GetAsistLayer(name)



        '以下命名规则为，与x轴相交的点的集合，叫做xpts

        ' Dim nx As Integer = (xcells + 1) * 2
        Dim ny As Integer = (ycells + 1) * 2

        ' Dim xpts(nx) As MapWinGIS.Point

        Dim ypts(ny) As MapWinGIS.Point

        Dim pt1 As MapWinGIS.Point
        Dim pt2 As MapWinGIS.Point

        'For i As Integer = 0 To xcells

        '    pt1 = New Point()
        '    pt1.x = xOrg + i * gridspace
        '    pt1.y = yOrg
        '    xpts(2 * i) = pt1

        '    pt2 = New Point()
        '    pt2.x = xOrg + i * gridspace
        '    pt2.y = (ycells) * gridspace + yOrg
        '    xpts(2 * i + 1) = pt2




        'Next

        For j As Integer = 0 To ycells


            pt1 = New Point()
            pt1.x = xOrg
            pt1.y = yOrg + j * yspace
            ypts(2 * j) = pt1

            pt2 = New Point()
            pt2.x = xcells * xspace + xOrg
            pt2.y = yOrg + j * yspace
            ypts(2 * j + 1) = pt2




        Next

        sfGrid.StartEditingShapes(True)

        'For k As Integer = 0 To xcells

        '    Dim shp As New Shape
        '    shp.ShapeType = ShpfileType.SHP_POLYLINE
        '    Dim ptIndex As Integer = 0

        '    shp.InsertPoint(xpts(2 * k), ptIndex)
        '    shp.InsertPoint(xpts(2 * k + 1), ptIndex)




        '    Dim shpIndex As Integer = sfGrid.NumShapes

        '    sfGrid.EditInsertShape(shp, shpIndex)

        '    sfGrid.EditCellValue(0, shpIndex, shpIndex)

        'Next


        For h As Integer = 0 To ycells

            Dim shp As New Shape
            shp.ShapeType = ShpfileType.SHP_POLYLINE
            Dim ptIndex As Integer = 0

            shp.InsertPoint(ypts(2 * h), ptIndex)
            shp.InsertPoint(ypts(2 * h + 1), ptIndex)




            Dim shpIndex As Integer = sfGrid.NumShapes

            sfGrid.EditInsertShape(shp, shpIndex)

            sfGrid.EditCellValue(0, shpIndex, shpIndex)

        Next




        sfGrid.StopEditingShapes()
        mapwin.Redraw()




    End Sub

    Public Sub DrawGridLineY(ByVal g As GridProp)
        Dim name As String = g.LnameProperty
        Dim xOrg As Double = g.LBPointXAProperty
        Dim yOrg As Double = g.LBPointYAProperty
        Dim xcells As Integer = g.XCellsProperty
        Dim ycells As Integer = g.YCellsProperty

        'Dim gridspace As Double
        Dim xspace As Double = g.XunitLProperty
        Dim yspace As Double = g.YuintLProperty


        Dim sfGrid As Shapefile

        sfGrid = Me.GetAsistLayer(name)



        '以下命名规则为，与x轴相交的点的集合，叫做xpts

        Dim nx As Integer = (xcells + 1) * 2
        ' Dim ny As Integer = (ycells + 1) * 2

        Dim xpts(nx) As MapWinGIS.Point

        ' Dim ypts(ny) As MapWinGIS.Point

        Dim pt1 As MapWinGIS.Point
        Dim pt2 As MapWinGIS.Point

        For i As Integer = 0 To xcells

            pt1 = New Point()
            pt1.x = xOrg + i * xspace
            pt1.y = yOrg
            xpts(2 * i) = pt1

            pt2 = New Point()
            pt2.x = xOrg + i * xspace
            pt2.y = (ycells) * yspace + yOrg
            xpts(2 * i + 1) = pt2




        Next

        'For j As Integer = 0 To ycells


        '    pt1 = New Point()
        '    pt1.x = xOrg
        '    pt1.y = yOrg + j * gridspace
        '    ypts(2 * j) = pt1

        '    pt2 = New Point()
        '    pt2.x = xcells * gridspace + xOrg
        '    pt2.y = yOrg + j * gridspace
        '    ypts(2 * j + 1) = pt2




        'Next

        sfGrid.StartEditingShapes(True)

        For k As Integer = 0 To xcells

            Dim shp As New Shape
            shp.ShapeType = ShpfileType.SHP_POLYLINE
            Dim ptIndex As Integer = 0

            shp.InsertPoint(xpts(2 * k), ptIndex)
            shp.InsertPoint(xpts(2 * k + 1), ptIndex)




            Dim shpIndex As Integer = sfGrid.NumShapes

            sfGrid.EditInsertShape(shp, shpIndex)

            sfGrid.EditCellValue(0, shpIndex, shpIndex)

        Next


        'For h As Integer = 0 To ycells

        '    Dim shp As New Shape
        '    shp.ShapeType = ShpfileType.SHP_POLYLINE
        '    Dim ptIndex As Integer = 0

        '    shp.InsertPoint(ypts(2 * h), ptIndex)
        '    shp.InsertPoint(ypts(2 * h + 1), ptIndex)




        '    Dim shpIndex As Integer = sfGrid.NumShapes

        '    sfGrid.EditInsertShape(shp, shpIndex)

        '    sfGrid.EditCellValue(0, shpIndex, shpIndex)

        'Next




        sfGrid.StopEditingShapes()
        mapwin.Redraw()

    End Sub

    '这里需要注意小数级别的网格
    Public Sub DrawLayerGrid(ByVal name As String, ByVal xOrg As Double, ByVal yOrg As Double, ByVal xcells As Integer, ByVal ycells As Integer, ByVal gridspace As Double)

        Dim sfGrid As Shapefile

        sfGrid = Me.GetAsistLayer(name)



        '以下命名规则为，与x轴相交的点的集合，叫做xpts

        Dim nx As Integer = (xcells + 1) * 2
        Dim ny As Integer = (ycells + 1) * 2

        Dim xpts(nx) As MapWinGIS.Point

        Dim ypts(ny) As MapWinGIS.Point

        Dim pt1 As MapWinGIS.Point
        Dim pt2 As MapWinGIS.Point

        For i As Integer = 0 To xcells

            pt1 = New Point()
            pt1.x = xOrg + i * gridspace
            pt1.y = yOrg
            xpts(2 * i) = pt1

            pt2 = New Point()
            pt2.x = xOrg + i * gridspace
            pt2.y = (ycells) * gridspace + yOrg
            xpts(2 * i + 1) = pt2




        Next

        For j As Integer = 0 To ycells


            pt1 = New Point()
            pt1.x = xOrg
            pt1.y = yOrg + j * gridspace
            ypts(2 * j) = pt1

            pt2 = New Point()
            pt2.x = xcells * gridspace + xOrg
            pt2.y = yOrg + j * gridspace
            ypts(2 * j + 1) = pt2




        Next

        sfGrid.StartEditingShapes(True)

        For k As Integer = 0 To xcells

            Dim shp As New Shape
            shp.ShapeType = ShpfileType.SHP_POLYLINE
            Dim ptIndex As Integer = 0

            shp.InsertPoint(xpts(2 * k), ptIndex)
            shp.InsertPoint(xpts(2 * k + 1), ptIndex)




            Dim shpIndex As Integer = sfGrid.NumShapes

            sfGrid.EditInsertShape(shp, shpIndex)

            sfGrid.EditCellValue(0, shpIndex, shpIndex)

        Next


        For h As Integer = 0 To ycells

            Dim shp As New Shape
            shp.ShapeType = ShpfileType.SHP_POLYLINE
            Dim ptIndex As Integer = 0

            shp.InsertPoint(ypts(2 * h), ptIndex)
            shp.InsertPoint(ypts(2 * h + 1), ptIndex)




            Dim shpIndex As Integer = sfGrid.NumShapes

            sfGrid.EditInsertShape(shp, shpIndex)

            sfGrid.EditCellValue(0, shpIndex, shpIndex)

        Next




        sfGrid.StopEditingShapes()
        mapwin.Redraw()






    End Sub
    Public Sub DrawLayerGridLable(ByVal name As String, ByVal xOrg As Double, ByVal yOrg As Double, ByVal xcells As Integer, ByVal ycells As Integer, ByVal gridspace As Double)

        Dim iLayer As Integer = GetAsistLayerId(name)
        Dim dVal As Double
        Dim x As Double
        Dim y As Double

        Dim uclr As UInt32 = RGB(0, 0, 0)


        'For i As Integer = 0 To xcells
        '    dVal = xOrg + i * gridspace
        '    x = dVal
        '    y = yOrg

        '    mapwin.AddLabelEx(iLayer, x.ToString(), uclr, x, y, tkHJustification.hjCenter, 0)


        'Next

        'For i As Integer = 0 To ycells
        '    dVal = yOrg + i * gridspace
        '    x = xOrg
        '    y = dVal

        '    mapwin.AddLabelEx(iLayer, y.ToString(), uclr, x, y, tkHJustification.hjLeft, 0)


        'Next


        For i As Integer = 0 To xcells
            dVal = i * gridspace
            x = xOrg + i * gridspace
            y = yOrg

            mapwin.AddLabelEx(iLayer, dVal.ToString(), uclr, x, y, tkHJustification.hjCenter, 0)


        Next


        '0 原点只需要写一次
        For i As Integer = 1 To ycells
            dVal = i * gridspace
            x = xOrg
            y = yOrg + i * gridspace

            mapwin.AddLabelEx(iLayer, dVal.ToString(), uclr, x, y, tkHJustification.hjLeft, 0)


        Next


        '设置大小
        mapwin.set_LayerLabelsScale(iLayer, True)

        mapwin.LayerFont(iLayer, "Times New Roman", 2)

        ' mapwin.set_LayerLabelsOffset(iLayer, 2)





    End Sub

    Public Sub DrawLayerGridLableAbsolute(ByVal name As String, ByVal xOrg As Double, ByVal yOrg As Double, ByVal xcells As Integer, ByVal ycells As Integer, ByVal gridspace As Double)

        Dim iLayer As Integer = GetAsistLayerId(name)
        Dim dVal As Double
        Dim x As Double
        Dim y As Double

        Dim uclr As UInt32 = RGB(0, 0, 0)


        For i As Integer = 0 To xcells
            dVal = xOrg + i * gridspace
            x = dVal
            y = yOrg

            mapwin.AddLabelEx(iLayer, x.ToString(), uclr, x, y, tkHJustification.hjCenter, -90)


        Next

        For i As Integer = 0 To ycells
            dVal = yOrg + i * gridspace
            x = xOrg
            y = dVal

            mapwin.AddLabelEx(iLayer, y.ToString(), uclr, x, y, tkHJustification.hjLeft, 0)


        Next


        'For i As Integer = 0 To xcells
        '    dVal = i * gridspace
        '    x = xOrg + i * gridspace
        '    y = yOrg

        '    mapwin.AddLabelEx(iLayer, dVal.ToString(), uclr, x, y, tkHJustification.hjCenter, 0)


        'Next


        ''0 原点只需要写一次
        'For i As Integer = 1 To ycells
        '    dVal = i * gridspace
        '    x = xOrg
        '    y = yOrg + i * gridspace

        '    mapwin.AddLabelEx(iLayer, dVal.ToString(), uclr, x, y, tkHJustification.hjLeft, 0)


        'Next


        '设置大小
        mapwin.set_LayerLabelsScale(iLayer, True)

        mapwin.LayerFont(iLayer, "Times New Roman", 2)







    End Sub

    Public Sub DrawLayerGridLableEx(ByVal name As String, ByVal xOrg As Double, ByVal yOrg As Double, ByVal xcells As Integer, ByVal ycells As Integer, ByVal gridspace As Double, ByVal xRe As Double, ByVal yRe As Double)

        '这里首先绘制坐标轴的标线

        Dim alen As Double = 1000 '标线的长度



        Dim sfGrid As Shapefile

        sfGrid = Me.GetAsistLayer(name)



        '以下命名规则为，与x轴相交的点的集合，叫做xpts

        Dim nx As Integer = (xcells + 1) * 2
        Dim ny As Integer = (ycells + 1) * 2

        Dim xpts(nx) As MapWinGIS.Point

        Dim ypts(ny) As MapWinGIS.Point

        Dim pt1 As MapWinGIS.Point
        Dim pt2 As MapWinGIS.Point

        For i As Integer = 0 To xcells

            pt1 = New Point()
            pt1.x = xOrg + i * gridspace
            pt1.y = yOrg
            xpts(2 * i) = pt1

            pt2 = New Point()
            pt2.x = xOrg + i * gridspace

            ' pt2.y = (ycells) * gridspace + yOrg
            pt2.y = yOrg - alen


            xpts(2 * i + 1) = pt2




        Next

        For j As Integer = 0 To ycells


            pt1 = New Point()
            pt1.x = xOrg
            pt1.y = yOrg + j * gridspace
            ypts(2 * j) = pt1

            pt2 = New Point()
            ' pt2.x = xcells * gridspace + xOrg

            pt2.x = xOrg - alen



            pt2.y = yOrg + j * gridspace

            ypts(2 * j + 1) = pt2





        Next

        sfGrid.StartEditingShapes(True)

        For k As Integer = 0 To xcells

            Dim shp As New Shape
            shp.ShapeType = ShpfileType.SHP_POLYLINE
            Dim ptIndex As Integer = 0

            shp.InsertPoint(xpts(2 * k), ptIndex)
            shp.InsertPoint(xpts(2 * k + 1), ptIndex)




            Dim shpIndex As Integer = sfGrid.NumShapes

            sfGrid.EditInsertShape(shp, shpIndex)

            sfGrid.EditCellValue(0, shpIndex, shpIndex)

        Next


        For h As Integer = 0 To ycells

            Dim shp As New Shape
            shp.ShapeType = ShpfileType.SHP_POLYLINE
            Dim ptIndex As Integer = 0

            shp.InsertPoint(ypts(2 * h), ptIndex)
            shp.InsertPoint(ypts(2 * h + 1), ptIndex)




            Dim shpIndex As Integer = sfGrid.NumShapes

            sfGrid.EditInsertShape(shp, shpIndex)

            sfGrid.EditCellValue(0, shpIndex, shpIndex)

        Next




        sfGrid.StopEditingShapes()








        Dim iLayer As Integer = GetAsistLayerId(name)





        Dim dVal As Double
        Dim x As Double
        Dim y As Double


        Dim xAdd As Double = 2000
        Dim yAdd As Double = 2000

        Dim uclr As UInt32 = RGB(0, 0, 0)


        For i As Integer = 0 To xcells
            dVal = xRe + i * gridspace
            x = xOrg + i * gridspace + xAdd
            y = yOrg

            mapwin.AddLabelEx(iLayer, dVal.ToString(), uclr, x, y, tkHJustification.hjRight, 0)


        Next

        For i As Integer = 0 To ycells
            dVal = yRe + i * gridspace
            x = xOrg - 2 * xAdd
            y = yOrg + i * gridspace + yAdd

            mapwin.AddLabelEx(iLayer, dVal.ToString(), uclr, x, y, tkHJustification.hjCenter, 0)


        Next


        'For i As Integer = 0 To xcells
        '    dVal = i * gridspace
        '    x = xOrg + i * gridspace
        '    y = yOrg

        '    mapwin.AddLabelEx(iLayer, dVal.ToString(), uclr, x, y, tkHJustification.hjCenter, 0)


        'Next


        ''0 原点只需要写一次
        'For i As Integer = 1 To ycells
        '    dVal = i * gridspace
        '    x = xOrg
        '    y = yOrg + i * gridspace

        '    mapwin.AddLabelEx(iLayer, dVal.ToString(), uclr, x, y, tkHJustification.hjLeft, 0)


        'Next


        '设置大小
        ' mapwin.set_LayerLabelsScale(iLayer, True)

        mapwin.LayerFont(iLayer, "Times New Roman", 14)











    End Sub

    Public Sub DrawAxis(ByVal g As AxisProp)
        If g.IsShowBottomProperty Then
            DrawAxisB(g)
        End If

        If g.IsShowLeftProperty Then
            DrawAxisL(g)
        End If

        If g.IsShowRightProperty Then
            DrawAxisR(g)
        End If

        If g.IsShowTopProperty Then
            DrawAxisT(g)
        End If


    End Sub
    Private Sub DrawAxisL(ByVal g As AxisProp)
        Dim name As String = g.LnameProperty
        Dim xOrg As Double = g.LBPointXAProperty
        Dim yOrg As Double = g.LBPointYAProperty
        Dim xcells As Integer = g.XCellsProperty
        Dim ycells As Integer = g.YCellsProperty

        'Dim gridspace As Double
        Dim xspace As Double = g.XunitLProperty
        Dim yspace As Double = g.YuintLProperty


        Dim xRe As Double = g.LBPointXProperty
        Dim yRe As Double = g.LBPointYProperty

        Dim alen As Double = g.ScaleLProperty

        '开始绘制啦

        Dim pt1 As MapWinGIS.Point
        Dim pt2 As MapWinGIS.Point




        Dim sfGrid As Shapefile

        sfGrid = Me.GetAsistLayer(name)

        '以下命名规则为，与x轴相交的点的集合，叫做xpts


        Dim ny As Integer = (ycells + 1) * 2



        Dim ypts(ny) As MapWinGIS.Point





        For j As Integer = 0 To ycells


            pt1 = New Point()
            pt1.x = xOrg
            pt1.y = yOrg + j * yspace
            ypts(2 * j) = pt1

            pt2 = New Point()
            ' pt2.x = xcells * gridspace + xOrg

            pt2.x = xOrg - alen



            pt2.y = yOrg + j * yspace

            ypts(2 * j + 1) = pt2





        Next

        sfGrid.StartEditingShapes(True)


        If g.IsShowScaleProperty Then



            For h As Integer = 0 To ycells

                Dim shp As New Shape
                shp.ShapeType = ShpfileType.SHP_POLYLINE
                Dim ptIndex As Integer = 0

                shp.InsertPoint(ypts(2 * h), ptIndex)
                shp.InsertPoint(ypts(2 * h + 1), ptIndex)




                Dim shpIndex As Integer = sfGrid.NumShapes

                sfGrid.EditInsertShape(shp, shpIndex)

                sfGrid.EditCellValue(0, shpIndex, shpIndex)

            Next


        End If


        '那条主轴线得画出来，真正的轴
        Dim shp1 As New Shape
        shp1.ShapeType = ShpfileType.SHP_POLYLINE
        Dim ptIndex1 As Integer = 0
        pt1 = New MapWinGIS.Point()
        pt1.x = xOrg
        pt1.y = yOrg

        pt2 = New MapWinGIS.Point()
        pt2.x = xOrg
        pt2.y = yOrg + ycells * yspace

        shp1.InsertPoint(pt1, ptIndex1)
        shp1.InsertPoint(pt2, ptIndex1)




        Dim shpIndex1 As Integer = sfGrid.NumShapes

        sfGrid.EditInsertShape(shp1, shpIndex1)

        sfGrid.EditCellValue(0, shpIndex1, shpIndex1)




        sfGrid.StopEditingShapes()


        If g.IsShowTextProperty Then




            '下面开始万恶的文字绘制
            Dim iLayer As Integer = GetAsistLayerId(name)

            Dim dVal As Double
            Dim x As Double
            Dim y As Double

            Dim uclr As UInt32 = RGB(g.LblColorProperty.R, g.LblColorProperty.G, g.LblColorProperty.B)

            For i As Integer = 0 To ycells
                dVal = yRe + i * yspace
                x = xOrg - alen
                y = yOrg + i * yspace

                '由于软件本身BUG所引入的纠正代码

                x = x - g.xScale * g.LblFontProperty.Size * 2
                y = y + g.xScale * g.LblFontProperty.Size * 0.8


                mapwin.AddLabelEx(iLayer, dVal.ToString(), uclr, x, y, tkHJustification.hjLeft, 0)


            Next

        End If

    End Sub
    Private Sub DrawAxisR(ByVal g As AxisProp)
        Dim name As String = g.LnameProperty
        Dim xOrg As Double = g.LBPointXAProperty
        Dim yOrg As Double = g.LBPointYAProperty
        Dim xcells As Integer = g.XCellsProperty
        Dim ycells As Integer = g.YCellsProperty

        'Dim gridspace As Double
        Dim xspace As Double = g.XunitLProperty
        Dim yspace As Double = g.YuintLProperty


        Dim xRe As Double = g.LBPointXProperty
        Dim yRe As Double = g.LBPointYProperty

        Dim alen As Double = g.ScaleLProperty

        '开始绘制啦
        Dim sfGrid As Shapefile

        sfGrid = Me.GetAsistLayer(name)

        '以下命名规则为，与x轴相交的点的集合，叫做xpts

        'Dim nx As Integer = (xcells + 1) * 2
        Dim ny As Integer = (ycells + 1) * 2

        ' Dim xpts(nx) As MapWinGIS.Point

        Dim ypts(ny) As MapWinGIS.Point

        Dim pt1 As MapWinGIS.Point
        Dim pt2 As MapWinGIS.Point



        For j As Integer = 0 To ycells


            pt1 = New Point()
            ' pt1.x = xOrg
            pt1.x = xOrg + xcells * xspace

            pt1.y = yOrg + j * yspace
            ypts(2 * j) = pt1

            pt2 = New Point()
            ' pt2.x = xcells * gridspace + xOrg

            ' pt2.x = xOrg - alen
            pt2.x = xOrg + alen + xcells * xspace



            pt2.y = yOrg + j * yspace

            ypts(2 * j + 1) = pt2





        Next

        sfGrid.StartEditingShapes(True)


        If g.IsShowScaleProperty Then



            For h As Integer = 0 To ycells

                Dim shp As New Shape
                shp.ShapeType = ShpfileType.SHP_POLYLINE
                Dim ptIndex As Integer = 0

                shp.InsertPoint(ypts(2 * h), ptIndex)
                shp.InsertPoint(ypts(2 * h + 1), ptIndex)




                Dim shpIndex As Integer = sfGrid.NumShapes

                sfGrid.EditInsertShape(shp, shpIndex)

                sfGrid.EditCellValue(0, shpIndex, shpIndex)

            Next

        End If


        '那条主轴线得画出来，真正的轴
        Dim shp1 As New Shape
        shp1.ShapeType = ShpfileType.SHP_POLYLINE
        Dim ptIndex1 As Integer = 0
        pt1 = New MapWinGIS.Point()
        pt1.x = xOrg + xspace * xcells
        pt1.y = yOrg

        pt2 = New MapWinGIS.Point()
        pt2.x = xOrg + xspace * xcells
        pt2.y = yOrg + ycells * yspace

        shp1.InsertPoint(pt1, ptIndex1)
        shp1.InsertPoint(pt2, ptIndex1)




        Dim shpIndex1 As Integer = sfGrid.NumShapes

        sfGrid.EditInsertShape(shp1, shpIndex1)

        sfGrid.EditCellValue(0, shpIndex1, shpIndex1)




        sfGrid.StopEditingShapes()


        If g.IsShowTextProperty Then



            '下面开始万恶的文字绘制
            Dim iLayer As Integer = GetAsistLayerId(name)

            Dim dVal As Double
            Dim x As Double
            Dim y As Double

            Dim uclr As UInt32 = RGB(g.LblColorProperty.R, g.LblColorProperty.G, g.LblColorProperty.B)

            For i As Integer = 0 To ycells
                dVal = yRe + i * yspace
                x = xOrg + xspace * xcells + alen
                y = yOrg + i * yspace


                '由于软件本身BUG所引入的纠正代码

                x = x + g.xScale * g.LblFontProperty.Size
                y = y + g.xScale * g.LblFontProperty.Size * 1.8

                mapwin.AddLabelEx(iLayer, dVal.ToString(), uclr, x, y, tkHJustification.hjRight, 0)


            Next
        End If



    End Sub
    Private Sub DrawAxisT(ByVal g As AxisProp)
        Dim name As String = g.LnameProperty
        Dim xOrg As Double = g.LBPointXAProperty
        Dim yOrg As Double = g.LBPointYAProperty
        Dim xcells As Integer = g.XCellsProperty
        Dim ycells As Integer = g.YCellsProperty

        'Dim gridspace As Double
        Dim xspace As Double = g.XunitLProperty
        Dim yspace As Double = g.YuintLProperty


        Dim xRe As Double = g.LBPointXProperty
        Dim yRe As Double = g.LBPointYProperty

        Dim alen As Double = g.ScaleLProperty

        '开始绘制啦
        Dim sfGrid As Shapefile

        sfGrid = Me.GetAsistLayer(name)

        '以下命名规则为，与x轴相交的点的集合，叫做xpts

        Dim nx As Integer = (xcells + 1) * 2


        Dim xpts(nx) As MapWinGIS.Point



        Dim pt1 As MapWinGIS.Point
        Dim pt2 As MapWinGIS.Point

        For i As Integer = 0 To xcells

            pt1 = New Point()
            pt1.x = xOrg + i * xspace
            pt1.y = yOrg + yspace * ycells
            xpts(2 * i) = pt1

            pt2 = New Point()
            pt2.x = xOrg + i * xspace

            ' pt2.y = (ycells) * gridspace + yOrg
            pt2.y = yOrg + alen + yspace * ycells


            xpts(2 * i + 1) = pt2




        Next

        sfGrid.StartEditingShapes(True)

        If g.IsShowScaleProperty Then

            For k As Integer = 0 To xcells

                Dim shp As New Shape
                shp.ShapeType = ShpfileType.SHP_POLYLINE
                Dim ptIndex As Integer = 0

                shp.InsertPoint(xpts(2 * k), ptIndex)
                shp.InsertPoint(xpts(2 * k + 1), ptIndex)




                Dim shpIndex As Integer = sfGrid.NumShapes

                sfGrid.EditInsertShape(shp, shpIndex)

                sfGrid.EditCellValue(0, shpIndex, shpIndex)

            Next

        End If


        '那条主轴线得画出来，真正的轴
        Dim shp1 As New Shape
        shp1.ShapeType = ShpfileType.SHP_POLYLINE
        Dim ptIndex1 As Integer = 0
        pt1 = New MapWinGIS.Point()
        pt1.x = xOrg
        pt1.y = yOrg + yspace * ycells

        pt2 = New MapWinGIS.Point()
        pt2.x = xOrg + xspace * xcells
        pt2.y = yOrg + ycells * yspace

        shp1.InsertPoint(pt1, ptIndex1)
        shp1.InsertPoint(pt2, ptIndex1)




        Dim shpIndex1 As Integer = sfGrid.NumShapes

        sfGrid.EditInsertShape(shp1, shpIndex1)

        sfGrid.EditCellValue(0, shpIndex1, shpIndex1)




        sfGrid.StopEditingShapes()



        If g.IsShowTextProperty Then



            '下面开始万恶的文字绘制
            '等有了比例尺，上面的轴兄弟你才能解脱啊
            Dim iLayer As Integer = GetAsistLayerId(name)

            Dim dVal As Double
            Dim x As Double
            Dim y As Double

            Dim uclr As UInt32 = RGB(g.LblColorProperty.R, g.LblColorProperty.G, g.LblColorProperty.B)

            For i As Integer = 0 To xcells
                dVal = xRe + i * xspace
                x = xOrg + i * xspace
                y = yOrg + ycells * yspace + alen

                '由于软件本身BUG所引入的纠正代码

                'x = x + g.xScale * g.LblFontProperty.Size
                y = y + g.xScale * g.LblFontProperty.Size * 1.8

                mapwin.AddLabelEx(iLayer, dVal.ToString(), uclr, x, y, tkHJustification.hjCenter, 0)


            Next
        End If

    End Sub
    Private Sub DrawAxisB(ByVal g As AxisProp)
        Dim name As String = g.LnameProperty
        Dim xOrg As Double = g.LBPointXAProperty
        Dim yOrg As Double = g.LBPointYAProperty
        Dim xcells As Integer = g.XCellsProperty
        Dim ycells As Integer = g.YCellsProperty

        'Dim gridspace As Double
        Dim xspace As Double = g.XunitLProperty
        Dim yspace As Double = g.YuintLProperty


        Dim xRe As Double = g.LBPointXProperty
        Dim yRe As Double = g.LBPointYProperty

        Dim alen As Double = g.ScaleLProperty

        '开始绘制啦
        Dim sfGrid As Shapefile

        sfGrid = Me.GetAsistLayer(name)

        '以下命名规则为，与x轴相交的点的集合，叫做xpts

        Dim nx As Integer = (xcells + 1) * 2


        Dim xpts(nx) As MapWinGIS.Point



        Dim pt1 As MapWinGIS.Point
        Dim pt2 As MapWinGIS.Point

        For i As Integer = 0 To xcells

            pt1 = New Point()
            pt1.x = xOrg + i * xspace
            pt1.y = yOrg
            xpts(2 * i) = pt1

            pt2 = New Point()
            pt2.x = xOrg + i * xspace

            ' pt2.y = (ycells) * gridspace + yOrg
            pt2.y = yOrg - alen


            xpts(2 * i + 1) = pt2




        Next

        sfGrid.StartEditingShapes(True)


        If g.IsShowScaleProperty Then

            For k As Integer = 0 To xcells

                Dim shp As New Shape
                shp.ShapeType = ShpfileType.SHP_POLYLINE
                Dim ptIndex As Integer = 0

                shp.InsertPoint(xpts(2 * k), ptIndex)
                shp.InsertPoint(xpts(2 * k + 1), ptIndex)




                Dim shpIndex As Integer = sfGrid.NumShapes

                sfGrid.EditInsertShape(shp, shpIndex)

                sfGrid.EditCellValue(0, shpIndex, shpIndex)

            Next

        End If

        '那条主轴线得画出来，真正的轴
        Dim shp1 As New Shape
        shp1.ShapeType = ShpfileType.SHP_POLYLINE
        Dim ptIndex1 As Integer = 0
        pt1 = New MapWinGIS.Point()
        pt1.x = xOrg
        pt1.y = yOrg

        pt2 = New MapWinGIS.Point()
        pt2.x = xOrg + xspace * xcells
        pt2.y = yOrg

        shp1.InsertPoint(pt1, ptIndex1)
        shp1.InsertPoint(pt2, ptIndex1)




        Dim shpIndex1 As Integer = sfGrid.NumShapes

        sfGrid.EditInsertShape(shp1, shpIndex1)

        sfGrid.EditCellValue(0, shpIndex1, shpIndex1)



        sfGrid.StopEditingShapes()


        If g.IsShowTextProperty Then



            '下面开始万恶的文字绘制
            Dim iLayer As Integer = GetAsistLayerId(name)

            Dim dVal As Double
            Dim x As Double
            Dim y As Double

            Dim uclr As UInt32 = RGB(g.LblColorProperty.R, g.LblColorProperty.G, g.LblColorProperty.B)

            For i As Integer = 0 To xcells
                dVal = xRe + i * xspace
                x = xOrg + i * xspace
                y = yOrg - alen

                mapwin.AddLabelEx(iLayer, dVal.ToString(), uclr, x, y, tkHJustification.hjCenter, 0)



            Next
        End If


    End Sub

    Public Sub RemoveAllShapesFromAsist(ByVal iLayer As Integer)
        Dim sfData As Shapefile = Me.GetAsistLayer(iLayer)
        If sfData Is Nothing Then
            Return
        End If


        sfData.StartEditingShapes(True)
      

        While sfData.NumShapes > 0
            sfData.EditDeleteShape(0)
        End While

        sfData.StopEditingShapes()

        mapwin.Redraw()
    End Sub



    Public Sub RemoveAllShapesFromPolutionLayer(ByVal iLayer As Integer)
        Dim sfData As Shapefile = Me.GetLayer(iLayer)
        If sfData Is Nothing Then
            Return
        End If


        sfData.StartEditingShapes(True)
        'For i As Integer = 0 To sfData.NumShapes - 1

        '    sfData.EditDeleteShape(i)

        'Next

        While sfData.NumShapes > 0
            sfData.EditDeleteShape(0)
        End While

        sfData.StopEditingShapes()

        mapwin.Redraw()
    End Sub








   






End Class
